<?php
class JourneysController extends AppController {

	function jlist(){
	    $uid = (int)$this->Session->read("userid");
	    if($uid==0)
	        $this->redirect("/");
		$journeys = $this->Journey->findAllByAssignedto($this->Session->read("userid"));
		$this->set('journeys',$journeys);

	}
	
	function plan($id=0){
	    $uid = (int)$this->Session->read("userid");
	    if($uid==0)
	        $this->redirect("/");
	    $this->layout='ajax';
	    if($id==0){
	    	$this->set("new","false");
	    	$this->set("id",$id);
	    	$this->set("journeyname","");
	    	$this->set("route","");
	    }else{
	        $journey= $this->Journey->findById($id);
	        if($journey['Journey']['assignedto']!=$uid)
	            $this->redirect("/");
	        if($journey){
		        $this->set("new","true");
		    	$this->set("id",$id);
		    	$this->set("journeyname",$journey['Journey']['journeyname']);
		    	$this->set("route",$journey['Journey']['route']);
	        }else{
		        $this->set("new","true");
		    	$this->set("id",$id);
		    	$this->set("journeyname","");
		    	$this->set("route","");
	        }
	    }
	}
	
	function view($id=0){
	    $uid = (int)$this->Session->read("userid");
	    if($uid==0)
	        $this->redirect("/");
	    $this->layout='ajax';
	    if($id==0){
	    	$this->redirect("/");
	    }else{
	        $journey= $this->Journey->findById($id);
	        if($journey['Journey']['assignedto']!=$uid)
	            $this->redirect("/");
	        if($journey){
		        $this->set("new","true");
		    	$this->set("id",$id);
		    	$this->set("journeyname",$journey['Journey']['journeyname']);
		    	$this->set("route",$journey['Journey']['route']);
	        }else{
		        $this->redirect("/");
	        }
	    }
	}
	
	function store($id,$journeyname,$route=""){
	    $uid = (int)$this->Session->read("userid");
	    if($uid==0)
	        exit;
	    //print "$id $journeyname $route"; exit;
	    $this->layout='ajax';
	    //Configure::write("debug",0);
	    if($id==0)
	    $this->Journey->create();
	    else
	    $this->Journey->id=$id;
	    $this->Journey->data['Journey']['journeyname']=$journeyname;
	    $this->Journey->data['Journey']['route']=$route;
	    $this->Journey->data['Journey']['assignedto']=$this->Session->read("userid");
	    $this->Journey->save($this->Journey->data);
	    echo $this->Journey->id;
	    exit;
	    
	}
}
?>
